namespace java edu.hawaii.ics.pjdb.types
include "misc.thrift"

// Use RemoteAddress from misc.thrift
typedef misc.RemoteAddress RemoteAddress

// Use Range from misc.thrift
typedef misc.Range Range
/**
 * Command instructing worker node to execute some SQL on its
 * local db. 
 */
struct DbSourceCommand {
    /**
     * The SQL to run on worker's local DB.
     */
    1: string sql;
}

/**
 * Similar to a DbSoruceCommand, but different in that it can
 * contain multiple SQL queries. This is useful for executing
 * DDLs on worker nodes.
 */
struct RunScriptCommand {
    /**
     * The script to run.
     */
    1: string script;
}

/**
 * DbEnrich receives tuples from the input location and does
 * a join on each one using its local db.
 */
struct DbEnrichCommand {
    /**
     * Address that we should /listen/ on to receive tuples to
     * join.
     */
    1: RemoteAddress inputAddress;
    
    /**
     * The field from the input tuples that we should be joining
     * on.
     */
    2: i32 joinFieldIndex;
    
    /**
     * The sql statement to run for each tuple we receive as 
     * input. Note that there should be exactly one binding, e.g.:
     * SELECT * FROM stocks WHERE user_id = ?
     */
    3: string sqlStatement;
}

/**
 * DbSync receives tuples from the input location and 
 * inserts them into the specified database.  Does not use
 * an output stream.
 */
struct DbSyncCommand {
    /**
     * Address that we should /listen/ on to receive tuples to
     * join.
     */
    1: RemoteAddress inputAddress;

    /**
     * The name of the table to insert the values into.
     * This table's schema must match the columns of the
     * tuples received.
     */
    2: string tableName;
}

/**
 * DbSyncRowSet receives tuples from the input location and 
 * inserts them into the specified database using rowsets. 
 * Does not use an output stream.
 */
struct DbSyncRowSetCommand {
    /**
     * Address that we should /listen/ on to receive tuples to
     * join.
     */
    1: RemoteAddress inputAddress;

    /**
     * The name of the table to insert the values into.
     * This table's schema must match the columns of the
     * tuples received.
     */
    2: string tableName;
}

/**
 * Command instructing worker node to get tuples from the
 * given CSV file.
 */
struct CsvSourceCommand {
    /**
     * The name of the CSV file to read.
     */
    1: string csvFile;
    
    /**
     * If the CSV file has a header that needs to be skipped.
     */
    2: bool header;
}

/**
 * Command instructing worker node to get tuples from the
 * given CSV file, partition the tuples, and send them
 * to the output streams.
 */
struct CsvPartitionCommand {
    /**
     * The name of the CSV file to read.
     */
    1: string csvFile;
    
    /**
     * If the CSV file has a header that needs to be skipped.
     */
    2: bool header;
    
    /**
     * The addresses that the partitions will be sent to.
     */
    3: list<RemoteAddress> outputAddresses;
    
    /**
     * The index of the field that will be used to partition the tuples.
     */
    4: i32 partitionFieldIndex;
    
    /**
     * The list of arguments needed by the partitioning function.
     */
    5: list<string> arguments;
    
    /**
     * Name of the partitioning function.
     */
    6: string partitionFunctionName;
}

/** 
 * Defines the various kinds of commands that can be run on a 
 * worker node.
 */
union Command {
    /** 
     * A command that instructs a worker to run a query on its
     * local database and return the results.
     */
    1: DbSourceCommand dbSourceCmd;
    
    /**
     * Similar to a DbSoruceCommand, but different in that it can
     * contain multiple SQL queries. This is useful for executing
     * DDLs on worker nodes.
     */
    2: RunScriptCommand runScriptCmd;
    
    /**
     * DbEnrich receives tuples from the input location and does
     * a join on each one using its local db.
     */
    3: DbEnrichCommand dbEnrichCmd;
    
    /**
     * DbSync receives tuples from the input location and 
     * inserts them into the specified database.  Does not use
     * an output stream.
     */
    4: DbSyncCommand dbSyncCmd;
    
    /**
     * DbSync receives tuples from the input location and 
     * inserts them into the specified database.  Does not use
     * an output stream.
     */
    5: DbSyncRowSetCommand dbSyncRowSetCmd;
    
    /**
     * CsvSource retrieves tuples from the given CSV file and
     * sends them to the output stream.
     */
    6: CsvSourceCommand csvSourceCmd;
    
    /**
     * Command instructing worker node to get tuples from the
     * given CSV file, partition the tuples, and send them
     * to the output streams.
     */
    7: CsvPartitionCommand csvPartitionCmd; 
}

/**
 * Encapsulates all of the information needed to send a worker a
 * command. WorkerCommand objects are serialized and sent to worker
 * nodes.
 */
struct WorkerCommand {
    /**
     * The address of the worker that the query runner should send
     * the command to.
     */
    1: RemoteAddress workerAddress;
    
    /**
     * Address that the worker should send output to.
     */
    2: RemoteAddress outputAddress;
    
    /**
     * Comamnd to for the worker to run.
     */
    3: Command command;
    
    /**
     * Path of the local database to connect to.
     */
     4: string DbPath;
}

/**
 * Contains the information of a Query Execution Plan.
 * Essentially a list of commands to be sent to the
 * worker nodes and executed.
 */
struct Plan {
    /*
     * The list of commands that should be sent to the
     * worker node.
     */
    1: list<WorkerCommand> commands;
}